<?php

class IndexAction extends Action{
    
    public function _initialize() {
        
    }
    
    public function index() {
        $this->display();
    }
    
    public function projects() {
        $cdate = I('timegap', 0);
        $sortype = I('sortype', 1);
        $prj = M('project')->order(array('ctime'))->limit(1)->select();
        $project = null;
        if ($prj) {
            $startime = 0;
            if ($cdate == 1) {
                $startime = $prj['ctime'] - 86400 * 7;
            }
            if ($cdate == 2) {
                $startime = $prj['ctime'] - 86400 * 30;
            }
            //分页查询
            $order = '';
            if ($sortype==1) {//已托管
                $order = '(case job.moneyin when 1 then 4 when 4 then 3 when 2 then 2 when 0 then 1 end) desc, job.ctime';
            }
            else if ($sortype==2) {//职位数最多
                $order = 'job.num-job.datastate desc, job.ctime';
            }
            else if ($sortype==3) {//薪酬最高
                $order = 'job.salary desc, job.ctime';
            }
            
            $tables = array('xia_job'=>'job', 'xia_project'=>'prj', 'xia_team'=>'team', 'xia_user_info'=>'uif', 'xia_account'=>'ac');//array('xia_project' => 'prj', 'xia_team' => 'team');
            $field = 'job.*,prj.id as pid,prj.name as pname,team.logo,team.weibo,team.weixin,team.favcount,uif.nickname as creater, ac.emailverify,ac.passcheck';
            $where = 'job.ctime>'.$startime.' and job.pid=prj.id and prj.state>0 and prj.tid=team.id and team.power=2 and team.uid=uif.uid and uif.uid=ac.uid and ac.passcheck<>0';
            import('ORG.Util.Page');// 导入分页类
            $count      = M('project')->table($tables)->field($field)->where($where)->count();// 查询满足要求的总记录数
            $Page       = new Page($count,20);// 实例化分页类 传入总记录数
            // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
            $nowPage = isset($_GET['p'])?$_GET['p']:1;
            $project = M('project')->table($tables)->field($field)->where($where)->page($nowPage.','.$Page->listRows)->order($order)->select();
            $show    = $Page->show();// 分页显示输出
            $this->assign('page',$show);// 赋值分页输出
        }
//        dump($project);
//        exit;
        $this->assign("projects", $project);
        
        $this->assign('menu', 1);
        $this->display();
    }
    
    public function prjs_ajax() {
        $startime = 0;
        $order = 'job.num-job.datastate desc, job.ctime';
        $tables = array('xia_job'=>'job', 'xia_project'=>'prj', 'xia_team'=>'team', 'xia_user_info'=>'uif', 'xia_account'=>'ac');//array('xia_project' => 'prj', 'xia_team' => 'team');
        $field = 'job.*,prj.id as pid,prj.name as pname,team.logo,team.weibo,team.weixin,team.favcount,uif.nickname as creater, ac.emailverify,ac.passcheck';
        $where = 'job.ctime>'.$startime.' and job.pid=prj.id and prj.state>0 and prj.tid=team.id and team.power=2 and team.uid=uif.uid and uif.uid=ac.uid and ac.passcheck<>0';
        import('ORG.Util.Page');// 导入分页类
        $count      = M('project')->table($tables)->field($field)->where($where)->order('job.ctime desc')->count();// 查询满足要求的总记录数
        $Page       = new Page($count,20);// 实例化分页类 传入总记录数
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $nowPage = isset($_GET['p'])?$_GET['p']:1;
        $project = M('project')->table($tables)->field($field)->where($where)->page($nowPage.','.$Page->listRows)->order($order)->select();
        
        $output = json_encode(array('page'=>$nowPage, 'projects'=>$project));
        echo $output;
    }
    
    public function projects2() {
        $this->assign('menu', 1);
        $this->display();
    }
    
    public function rencai() {
        $cdate = I('timegap', 0);
        $startime = 0;
        $sortype = I('sortype', 2);//默认按工作资历排序
        if ($cdate == 1) {
            $startime = time() - 86400 * 7;
        }
        if ($cdate == 2) {
            $$startime = time() - 86400 * 30;
        }
        if ($sortype==1) {
            $order = "xia_user_info.teamid, xia_account.infopers desc, xia_user_info.uptime desc";
        }
        else if ($sortype==2) {
            $order = "xia_user_info.exp desc, xia_account.infopers desc, xia_user_info.uptime desc";
        }
        
        $table = 'xia_user_info,xia_account';
        $where = "xia_user_info.utype=1 and xia_user_info.uptime>' . $startime . ' and xia_user_info.uid=xia_account.uid and xia_account.passcheck=1 and xia_account.emailverify=1";
        import('ORG.Util.Page');// 导入分页类
        $count      = M('user_info')->table($table)->where($where)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,20);// 实例化分页类 传入总记录数
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $nowPage = isset($_GET['p'])?$_GET['p']:1;
        $rencai = M('user_info')->table($table)->where($where)->page($nowPage.','.$Page->listRows)->order($order)->select();
        //隐私过滤
        for ($i=0;$i<count($rencai);$i++) {
            userInfoFilter($this->userinfo, $rencai[$i]);
            //Log::write('rencai:'. implode(',', $rencai[$i]), Log::ERR);
        }

        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("rencai", $rencai);
        $this->assign('menu', 2);
        $this->display();
    }
    
    public function rencai_ajax() {
        $startime = 0;
        $order = "xia_user_info.exp desc, xia_account.infopers desc, xia_user_info.uptime desc";
        $table = 'xia_user_info,xia_account';
        $where = "xia_user_info.utype=1 and xia_user_info.uptime>' . $startime . ' and xia_user_info.uid=xia_account.uid and xia_account.passcheck=1 and xia_account.emailverify=1";
        import('ORG.Util.Page');// 导入分页类
        $count      = M('user_info')->table($table)->where($where)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,20);// 实例化分页类 传入总记录数
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $nowPage = isset($_GET['p'])?$_GET['p']:1;
        $rencai = M('user_info')->table($table)->where($where)->page($nowPage.','.$Page->listRows)->order($order)->select();
        //隐私过滤
        for ($i=0;$i<count($rencai);$i++) {
            userInfoFilter($this->userinfo, $rencai[$i]);
            //Log::write('rencai:'. implode(',', $rencai[$i]), Log::ERR);
        }
        $show       = $Page->show();// 分页显示输出
        
        $output = json_encode(array('page'=>$nowPage, 'rencai'=>$rencai));
        echo $output;
    }
    
    public function rencai2() {
        $this->assign('menu', 2);
        $this->display();
    }
    
    public function touser() {
        $this->assign('menu', 0);
        $this->display();
    }
}
